export default {
  downloadBlob(blob, fileName, contentType) {
    const blobObj = new Blob([blob], {
      type: contentType
    })
    const link = document.createElement('a')
    link.download = fileName
    link.style.display = 'none'
    link.href = URL.createObjectURL(blobObj)
    document.body.appendChild(link)
    link.click()
    URL.revokeObjectURL(link.href)
    document.body.removeChild(link)
  },
  transToCapitalizedFirstLetter(string = '', forceUseFirst = false) {
    if (string !== '') {
      // 如果全是大写字母直接返回
      if (string === string.toUpperCase() && !forceUseFirst) {
        return string
      } else {
        // 分割后首字母大写
        let result = ''
        const strings = string.split(' ')
        strings.forEach(str => {
          result = result + str.split('')[0].toUpperCase()
        })
        return result
      }
    }
  },
  transformNullValue(value, notZero = false) {
    return !value && (value !== 0 || notZero) ? '--' : value
  },
  getRegex(type) {
    let regex
    if (type === 'url') {
      // eslint-disable-next-line prefer-regex-literals
      regex = new RegExp('^(?:(http|https)?:\\/\\/)?(?:[\\w-]+\\.)+([a-z]|[A-Z]|[0-9]){2,10}\\/?$', 'i')
    }
    return regex
  },
  numberToMonth(number, abbr = false) {
    const mouth = [
      'January',
      'February',
      'March',
      'April',
      'May',
      'June',
      'July',
      'August',
      'September',
      'October',
      'November',
      'December'
    ]
    if (abbr) {
      return mouth[number].substr(0, 3)
    }
    return mouth[number]
  },
  // 转千分位
  thousandsMark(value) {
    const splitNumber = value.split('.')
    if (splitNumber.length === 1) {
      return `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
    } else {
      return `${splitNumber[0]}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',') + `.${splitNumber[1]}`
    }
  }
}
